package com.sunshinew.controller;

import com.sunshinew.entity.User;
import com.sunshinew.service.UserService;
import com.sunshinew.utils.ResultObj;
import com.sunshinew.utils.WebUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author xiaomingxing
 * @create 2021/4/6 14:11
 */
@Slf4j
@Controller
public class LoginController {
    @Autowired
    private UserService userService;

    @GetMapping("index")
    public String index(){
        return "index";
    }

    @GetMapping({"/"})
    public String indexPage() {
        return "login";
    }

    @PostMapping("/login")
    @ResponseBody
    public ResultObj login(String username, String password) {
        log.info("username: {}, password: {}", username, password);
        User user = userService.getUserByUserName(username);
        if (user != null) {
            if (user.getPassword().equals(password)) {
                // 存入session域中
                WebUtils.getSession().setAttribute("user",user);
                log.info("登录成功！");
                return ResultObj.LOGIN_SUCCESS;
            }
        }
        log.info("账号或密码错误！");
        return ResultObj.LOGIN_ERROR_PASS;
    }
}
